#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDatabase>
#include "udpServer.h"

void mysqltest()
{
    QSqlError err;
    // 数据库对象创建
    //QSqlDatabase db = QSqlDatabase::addDatabase("MYSQL","ad");
    // 设置相关参数
    //        db.setDatabaseName("db");    // 需要与数据库中的一致
    //        db.setHostName("127.0.0.1");    // 主机名(地址)
    //        db.setPort(3366);        // 端口
    //打开MySQL
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");  //设置主机地址
    db.setPort(3306);  //设置端口
    db.setDatabaseName("demo_xfs");  //设置数据库名称
    db.setUserName("root");  //设置用户名
    db.setPassword("tjbus01");  //设置密码
    // 打开数据库
    if (!db.open()) {
        qDebug()<<"failed to connect to mysql";
        err = db.lastError();
    }
    qDebug()<<"success";
    // Query对象创建
    //QSqlQuery q("", db);
    // 创建一个表
    //q.exec("create table Names (id integer primary key, Firstname varchar, Lastname varchar, City varchar)");
    // 插入三条数据
//    q.exec("insert into Names values (1, 'Christopher', 'Walker', 'Morristown')");
//    q.exec("insert into Names values (2, 'Donald', 'Duck', 'Andeby')");
//    q.exec("insert into Names values (3, 'Buck', 'Rogers', 'Paris')");
    QSqlQuery query;
    bool success;
    success = query.exec("INSERT INTO demo_xfs (posid,water,turn,position_x,position_y,electricity,"
               "network,progVer,binVer,IMEI,IMSI,working,date)VALUE('abcdef',0,1,"
               "'0.0','0.0',0,0,'0','0','0','0',0,'2018-1-18 15:42:00')");
    if (!success) {
        qDebug()<<"failed";
    }
}
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterType<UdpServer>("Qt.UdpServer", 1, 0, "UdpServer");
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    //mysqltest();
    return app.exec();
}
